package com.yanqu.road.dao.impl.kuang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.enums.kuang.eKuangAttributeType;
import com.yanqu.road.entity.kuang.KuangEquipmentData;
import com.yanqu.road.entity.kuang.KuangUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KuangUserDataDaoImpl extends TempDataObjectDao<KuangUserData> {

    public static String getKuangUserDataSql = "select * from `t_u_kuang_user_data` where `userId`=?";

    public KuangUserData getKuangUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getKuangUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateKuangUserDataSql = "update `t_u_kuang_user_data` set weekId=?,groupId=?,roundDay=?,campId=?,equipmentDataMap=?,chooseEquipmentDataList=?,drawTimes=?,extraDrawTimes=?,totalDrawTimes=?,attributeMap=?,lastRoundScore=?,curioTimes=? where userId=?";

    @Override
    protected boolean updateData(KuangUserData kuangUserData) {
        kuangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getRoundDay()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getEquipmentDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getChooseEquipmentDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getExtraDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getAttributeMap())));
        para.put(i++, new DbParameter(Types.BIGINT, kuangUserData.getLastRoundScore()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getCurioTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, kuangUserData.getUserId()));
        if (getDb().executeNoneQuery(updateKuangUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertKuangUserDataSql = "insert into `t_u_kuang_user_data`(userId,weekId,groupId,roundDay,campId,equipmentDataMap,chooseEquipmentDataList,drawTimes,extraDrawTimes,totalDrawTimes,attributeMap,lastRoundScore,curioTimes) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(KuangUserData kuangUserData) {
        kuangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, kuangUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getRoundDay()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getEquipmentDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getChooseEquipmentDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getExtraDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(kuangUserData.getAttributeMap())));
        para.put(i++, new DbParameter(Types.BIGINT, kuangUserData.getLastRoundScore()));
        para.put(i++, new DbParameter(Types.INTEGER, kuangUserData.getCurioTimes()));
        if (getDb().executeNoneQuery(insertKuangUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected KuangUserData parseData(ResultSet rs) throws SQLException {
        KuangUserData kuangUserData = new KuangUserData();
        kuangUserData.setUserId(rs.getLong("userId"));
        kuangUserData.setWeekId(rs.getInt("weekId"));
        kuangUserData.setGroupId(rs.getInt("groupId"));
        kuangUserData.setRoundDay(rs.getInt("roundDay"));
        kuangUserData.setCampId(rs.getInt("campId"));
        String equipmentDataMap = rs.getString("equipmentDataMap");
        if (!StringUtils.isNullOrEmpty(equipmentDataMap)) {
            kuangUserData.setEquipmentDataMap(JSON.parseObject(equipmentDataMap, new TypeReference<Map<Integer, KuangEquipmentData>>() {
            }));
        }
        String chooseEquipmentDataList = rs.getString("chooseEquipmentDataList");
        if (!StringUtils.isNullOrEmpty(chooseEquipmentDataList)) {
            kuangUserData.setChooseEquipmentDataList(JSON.parseObject(chooseEquipmentDataList, new TypeReference<List<KuangEquipmentData>>() {
            }));
        }
        kuangUserData.setDrawTimes(rs.getInt("drawTimes"));
        kuangUserData.setExtraDrawTimes(rs.getInt("extraDrawTimes"));
        kuangUserData.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        String attributeMap = rs.getString("attributeMap");
        if (!StringUtils.isNullOrEmpty(attributeMap)) {
            kuangUserData.setAttributeMap(JSON.parseObject(attributeMap, new TypeReference<Map<eKuangAttributeType, Long>>() {
            }));
        }
        kuangUserData.setLastRoundScore(rs.getLong("lastRoundScore"));
        kuangUserData.setCurioTimes(rs.getInt("curioTimes"));
        return kuangUserData;
    }
}
